stages:
  - test
  - deploy
  - release

variables:
  DB: "mysqli"
  MYSQL_ROOT_PASSWORD: "superrootpass"
  TRAVIS_BUILD_DIR: "$CI_PROJECT_DIR"
  MOODLE_DIR: "/var/www/html/moodle"
  # If we want to ignore some templates, provide them here.
  #MUSTACHE_IGNORE_NAMES: "<file>.mustache, <file2>.mustache, ..., <filen>.mustache"
  PLUGIN: availability_gwpayments

.job_template: &job_definition
  stage: test
  services:
    - mysql:5.7
  cache:
    paths:
      - $HOME/.composer/cache
      - $HOME/.npm
  allow_failure: false
  image: git.sebsoft.nl:5050/sebsoft/open-source/moodle-cidocker:latest
  script:
    - cd /root/
    - umask u+x
    - cd $CI_PROJECT_DIR/..
    - source /root/.nvm/nvm.sh
    - /root/ci/bin/moodle-plugin-ci install --moodle=$MOODLE_DIR --db-user=root --db-pass=superrootpass --db-host=mysql -vvv
    # Start actually running the CI now.
    - exit_code+=(0)
    - /root/ci/bin/moodle-plugin-ci phplint || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci phpcpd || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci phpmd || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci codechecker || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci validate || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci savepoints || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci mustache || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci grunt || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci phpdoc || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci phpunit || exit_code+=($?)
    - /root/ci/bin/moodle-plugin-ci behat || exit_code+=($?)
    - for i in ${exit_code[@]}; do echo "Exit code ${exit_code[$i]}"; if [ "${exit_code[$i]}" -gt 0 ]; then exit 1; fi; done;

Moodle 3.11:
  <<: *job_definition
  variables:
    MOODLE_BRANCH: "MOODLE_311_STABLE"

Moodle 3.10:
  <<: *job_definition
  variables:
    MOODLE_BRANCH: "MOODLE_310_STABLE"

deploy.gitlab:
  stage: deploy
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG                  # Run this job when a tag is created manually
  script:
    - echo 'running deploy to create release in Gitlab'
  release:
    name: 'Release $CI_COMMIT_TAG'
    description: './CHANGES.md'  # Just read the changes from changes.md
    tag_name: '$CI_COMMIT_TAG'
    ref: '$CI_COMMIT_TAG'

deploy.github:
  stage: deploy
  image: moodlehq/moodle-php-apache:7.4
  script: 
    - echo "Deploying to Github..."
    - git checkout -b main
    - if [ "$(git remote | grep github | wc -l)" -eq "0" ]; then git remote add github $PUSHLOCATION; fi
    - git branch -M main
    - git push -u github main
    - git push -u github --tags
    - echo "Deploy to Github completed..."
  only:
     - tags

# After the deploy has been done, it's ready to be released to Moodle.org.
releaseto.moodle.org:
  image: php:7.4-cli
  stage: release
  rules:
    - if: $CI_COMMIT_TAG
  artifacts:
    paths:
      - /tmp/newbuild.zip
    expire_in: 1 week
  script:
    - echo "Releasing this to Moodle.org"
    - apt-get update
    - apt-get -y install zip curl jq
    - mkdir -p /tmp/release/$PLUGIN
    - cp -Rfv ./ /tmp/release/$PLUGIN/
    - cd /tmp/release
    - zip -r /tmp/newbuild.zip ./ -x '*.git*'
    - ITEMID=$(curl -s -F data=@/tmp/newbuild.zip "https://moodle.org/webservice/upload.php?token=$MOODLE_ORG_TOKEN" | jq --raw-output '.[0].itemid')
    - curl -s https://moodle.org/webservice/rest/server.php --data-urlencode "wstoken=${MOODLE_ORG_TOKEN}" --data-urlencode "wsfunction=local_plugins_add_version" --data-urlencode "moodlewsrestformat=json" --data-urlencode "frankenstyle=${PLUGIN}" --data-urlencode "zipdrafitemtid=${ITEMID}" | jq
